package cn.tedu.lucky.draw.server.controller;

import cn.tedu.lucky.draw.server.common.annotation.OperationLog;
import cn.tedu.lucky.draw.server.common.result.JsonResult;
import cn.tedu.lucky.draw.server.protocol.po.Activity;
import cn.tedu.lucky.draw.server.protocol.po.Prize;
import cn.tedu.lucky.draw.server.protocol.query.ActivityQuery;
import cn.tedu.lucky.draw.server.service.IActivityService;
import cn.tedu.lucky.draw.server.service.IPrizeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.json.JSONException;
import org.springframework.batch.core.*;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
@CrossOrigin
@RestController
@Api(tags = "活动管理模块")
public class ActivityController {

    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    private Job csvToDBJob;

    @Autowired
    private JobExplorer jobExplorer;

    @Autowired
    private IActivityService activityService;

    @Autowired
    private IPrizeService prizeService;

    @PostMapping("/draw/activity/getAll")
    public JsonResult<List<Activity>> findAll(@RequestBody ActivityQuery activityQuery) {
        List<Activity> activities = activityService.findAll(activityQuery);
        return JsonResult.ok(activities);
    }

    @OperationLog(operation = "新增活动")
    @PostMapping("/draw/activity/save")
    public JsonResult<Void> saveActivity(@RequestBody Activity activity){
        activityService.save(activity);
        return JsonResult.ok();
    }

    @GetMapping("/csv")
    public String csvToDb() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
        /**
         * jobExplorer 用于查询作业执行历史，获得下一个作业执行时所需要使用的参数，可以确保每次作业执行都会有独特的参数
         */
        JobParameters jobParameters = new JobParametersBuilder(new JobParameters(), jobExplorer)
                        .getNextJobParameters(csvToDBJob).toJobParameters();
        JobExecution run = jobLauncher.run(csvToDBJob, jobParameters);
        return run.getJobId().toString();
    }

    @PostMapping("/draw/activity/{id}")
    public JsonResult<Activity> getActivityById(@PathVariable("id") Long id) {
        Activity activity = activityService.findActivityById(id);
        return JsonResult.ok(activity);
    }

    @GetMapping("/draw/prize/findPrize/{id}")
    public JsonResult<List<Prize>> findPrizeByActivityId(@PathVariable("id") Long id) {
        List<Prize> prizeList=prizeService.findPrizeByActivityId(id);
        return JsonResult.ok(prizeList);
    }

    @GetMapping("/app/draw/activity/getAll")
    @ApiOperation(value = "获取APP端的活动列表")
    public JsonResult<List<Activity>> getAll(){
        List<Activity> activities = activityService.findAll();
        return JsonResult.ok(activities);
    }

    @GetMapping("/app/draw/activity/trigger/{id}")
    @ApiOperation(value = "抽奖接口")
    public JsonResult<Prize> trigger(@PathVariable("id") Long activityId) throws JSONException {
        Prize prize=activityService.trigger(activityId);
        return JsonResult.ok(prize);
    }
}
